package com.znxs.bs_c.service.impl;

import com.znxs.bs_c.entity.Menu;
import com.znxs.bs_c.mapper.MenuMapper;
import com.znxs.bs_c.service.IMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 左拿
 * @since 2023-08-21
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    @Override
    public List<Menu> getTreeMenuList(List<Menu> menuList) {
        // 找出pid 为null 的一级菜单目录
        List<Menu> parentNode = menuList.stream().filter(menu -> menu.getPid() == null).collect(Collectors.toList());
        // 找出一级菜单的子菜单
        for (Menu menu : parentNode) {
            // 筛选所有数据中 pid = 父级id的数据 就是二级菜单
            menu.setChildren(menuList.stream().filter(m -> menu.getId().equals(m.getPid())).collect(Collectors.toList()));
        }
        return parentNode;
    }
}
